/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * TrainingBook
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * Projects is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Projects is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "activity-tree-view.hpp"
#include <sstream>

///@brief Default constructor
ActivityTreeView::ActivityTreeView()
{
    ///@brief Create the tree model
    refActivityListTreeModel = Gtk::ListStore::create(activityColumns);
    set_model(refActivityListTreeModel);

    ///@brief Add view columns
    append_column("Date", activityColumns.m_col_date);
    append_column("Heure", activityColumns.m_col_time);
    append_column("Type", activityColumns.m_col_activitytype);
    append_column_numeric("Distance", activityColumns.m_col_distance, "%3.1f");
    append_column("Durée", activityColumns.m_col_duration);
    append_column("FC moy", activityColumns.m_col_fcmoy);
    append_column("FC max", activityColumns.m_col_fcmax);
    append_column("Qualité", activityColumns.m_col_quality);
    append_column("Calories", activityColumns.m_col_calory);
}

///@brief Destructor
ActivityTreeView::~ActivityTreeView()
{

}

///@brief Add a new activity to the TreeView
///@param act Activity to add
void ActivityTreeView::AddActivity(ActivityHeader act)
{
    Gtk::TreeModel::iterator iter = refActivityListTreeModel->append();
    Gtk::TreeModel::Row row = *iter;
    row[activityColumns.m_col_distance] = act.GetDistance();
    row[activityColumns.m_col_activitytype] = act.GetActivityType();
    row[activityColumns.m_col_duration] = act.GetDuration().AsText();
    row[activityColumns.m_col_date] = act.GetDateTime().AsString("dd/mm/yyyy");
    row[activityColumns.m_col_time] = act.GetDateTime().AsString("hh:nn:ss");
    row[activityColumns.m_col_fcmoy] = act.GetFCMoy();
    row[activityColumns.m_col_fcmax] = act.GetFCMax();
    row[activityColumns.m_col_calory] = act.GetCalories();
    row[activityColumns.m_col_quality] = act.GetQuality();
	row[activityColumns.m_col_id] = act.GetId();
}

///@brief Clear the TreeView
void ActivityTreeView::Clear()
{
    refActivityListTreeModel->clear();
}

///@brief Get the date in string format from a row
DateTime ActivityTreeView::GetDate()
{
    DateTime res;
    Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
    if(refSelection)
    {
        Gtk::TreeModel::iterator iter = refSelection->get_selected();
        if(iter)
        {
            std::ostringstream stream;
            stream << (*iter)[activityColumns.m_col_date];
            stream << "--";
            stream << (*iter)[activityColumns.m_col_time];
            res.SetWithFormat(stream.str(), "dd/mm/yyyy--hh:nn:ss");
        }
    }
    return res;
}

///@brief Return true if a line is selected
bool ActivityTreeView::LineSelected()
{
    if (get_selection()->get_selected())
    {
        return true;
    }
    return false;
}

///@brief Return id of activity in a given row
std::string ActivityTreeView::GetId()
{
	Glib::RefPtr<Gtk::TreeView::Selection> refSelection = get_selection();
    if(refSelection)
    {
        Gtk::TreeModel::iterator iter = refSelection->get_selected();
        if(iter)
        {
			std::ostringstream stream;
            stream << (*iter)[activityColumns.m_col_id];
            return stream.str();
        }
    }
    return " ";
}